--[[
Author: Blus
Time:2020.11.09
Desc:family info ui for pet
]]

require "Common/define"
require "Logic/PetManager"

local PetSelect = {}
HelperFunc.SetUIEnv(PetSelect)
local this = PetSelect

this.petSelected = 0

this.hasInit = false
this.backBtn = nil
this.Btn_BGClose = nil
this.petNameLabel = nil
this.drawWrapper = nil
this.Model_Pet = nil
this.PetModelGO = nil
this.partList = nil
this.petList = nil
this.Btn_Confirm = nil
this.Txt_PetCount = nil
this.selectedPetItemIndex = 1

function PetSelect.Init(in_type,in_petList)
    this.petSelectType = in_type
    if nil == in_petList then
        this.petInfoList = PetManager.PetInfoList
    else
        this.petInfoList = in_petList
    end
    --petTable = t
    --petTag = tag
    --petInSelect = petList
    local name, packageName = getUIName(PanelNames.PetSelect)
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI)
end

--定义控件
function PetSelect.OnCreate(obj)
    if not hasInit then
        hasInit = true
        backBtn = obj:GetChild("Btn_Back")

        Btn_BGClose = obj:GetChild("Btn_BGClose")
        petNameLabel = obj:GetChild("PetName")
        this.Model_Pet = obj:GetChild("Model_Pet")
        petList = obj:GetChild("PetList"):GetChild("List_Pet");
        partList = obj:GetChild("PartList")
        Btn_Confirm = obj:GetChild("Btn_Confirm")
        this.Txt_PetCount = obj:GetChild("Txt_PetCount")
        this.petList.itemRenderer = this.renderPetListItem
        this.partList.itemRenderer = this.renderPartListItem
        this.initModelTable()
    end
    this.addEvent()
    this.LocalListenEvent()

    this.showPanel()
end

function PetSelect.OnDestroy()
    this.removePetModel()
    this.LocalCloseEvent()
    this.removeEvent()
    this.removeModelTable()
    this.hasInit = false
end

function PetSelect.addEvent()
    backBtn.onClick:Add(this.OnCloseClick)
    Btn_BGClose.onClick:Add(this.OnCloseClick)
    Btn_Confirm.onClick:Add(this.OnOKClick)
    petList.onClickItem:Add(this.OnChangeSelectedPet)
end

function PetSelect.removeEvent()
    backBtn.onClick:Clear()
    Btn_BGClose.onClick:Clear()
    Btn_Confirm.onClick:Clear()
    petList.onClickItem:Remove(this.OnChangeSelectedPet)
end

function PetSelect.LocalListenEvent()
end

function PetSelect.LocalCloseEvent()
    Btn_BGClose.onClick:Remove(this.OnCloseClick)
    Btn_Confirm.onClick:Remove(this.OnOKClick)
    petList.onClickItem:Remove(this.OnChangeSelectedPet)
end

function PetSelect.OnOKClick()
    this.ClosePanel()
    if this.petSelectType == BrokerUtils.PETSHOP_PetType then
        if PetManager.IsTemplatePet then
            BrokerManager.BrokerAddedPetId = PetManager.TemplatePetId
        else
            BrokerManager.BrokerAddedPetId = PetManager.MutatePetId
        end
    end
    FireEvent(Config.EventType.SelectPet)
end

function PetSelect.OnCloseClick()
    this.ClosePanel()
    FireEvent(Config.EventType.SelectPet)
end

function this.ClosePanel()
    PetSelect.OnDestroy()
    closeUI(PanelNames.PetSelect)
end

function this.showPanel()
    if PetManager.IsTemplatePet then
        PetManager.TemplatePetId = 0
    else
        PetManager.MutatePetId = 0
    end
    this.selectedPetItemIndex = PetManager.GetNormalPetIndex()
    petList.numItems = #(this.petInfoList)
    this.showPartList()
end
function this.showPartList()
    local _touchable = false
    local _partCount = 0
    if this.selectedPetItemIndex == -1 then
        _touchable = false
        this.petNameLabel.text = ""
    else
        _touchable = true
        local _petInfo = PetManager.GetPetInfoByIndex(this.selectedPetItemIndex)
        _partCount = #_petInfo.genes
        this.petNameLabel.text = _petInfo.petName

        if PetManager.IsTemplatePet then
            if PetManager.TemplatePetId == 0 then
                PetManager.TemplatePetId = _petInfo.petId
            end
        else
            if PetManager.MutatePetId == 0 then
                PetManager.MutatePetId = _petInfo.petId
            end
        end
        this.showPetModel()
    end
    this.Btn_Confirm.touchable = _touchable
    this.Btn_Confirm.enabled = _touchable

    this.partList.numItems = _partCount

end

function this.renderPartListItem(index, obj)
    local _petInfo = PetManager.GetPetInfoByIndex(this.selectedPetItemIndex)
    local _geneId = _petInfo.genes[index + 1]
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.PetPartItem);
    ctrlItem.initData(obj, _geneId);
end

function this.renderPetListItem(index, obj)
    local _petInfo = PetManager.GetPetInfoByIndex(index + 1)
    local _selectedIndex = PetManager.GetPetItemType(_petInfo)
    obj:GetController("type"):SetSelectedIndex(_selectedIndex)
    obj:GetController("Lock"):SetSelectedIndex(_petInfo.IsLocked and 0 or 1)

    local _isNormal = true
    local _index = _isNormal and 0 or 1
    obj:GetController("cantSelect"):SetSelectedIndex(_index)

    obj.selected = this.selectedPetItemIndex == index + 1

    --显示宠物模型
    local _petListIndex = index + 1
    local _parentGO = obj:GetChild("Model_Pet")
    local _petId = _petInfo.petId
    local _genes = _petInfo.genes
    this.showFamilyTreePetModel(_petListIndex, _parentGO, _petId, _genes)

    local _petMutationLevel = PetManager.GetMutationLevel(_petInfo.genes)
    obj:GetController("level"):SetSelectedIndex(_petMutationLevel - 1)
    obj:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
end

function this.OnChangeSelectedPet(context)
    local obj = context.data
    local index = petList:GetChildIndex(obj)
    local _petInfo = PetManager.GetPetInfoByIndex(index + 1)
    local _isNormal = true

    if not _isNormal then
        UISysTips.AddMsg(getLanguage("PetMating_CannotMating"))
        obj.selected = false
        if this.selectedPetItemIndex >= 1 then
            petList:GetChildAt(this.selectedPetItemIndex - 1).selected = true
        end
        return
    end
    local _index = _isNormal and 0 or 1
    obj:GetController("cantSelect"):SetSelectedIndex(_index)
    this.selectedPetItemIndex = index + 1
    if PetManager.IsTemplatePet then
        PetManager.TemplatePetId = _petInfo.petId
    else
        PetManager.MutatePetId = _petInfo.petId
    end
    this.showPartList()
end

--显示宠物模型
function this.removePetModel()
    if this.PetModelGO then
        destroy(this.PetModelGO)
    end
    this.PetModelGO = nil
end
function this.showPetModel()
    local _petInfo = PetManager.GetPetInfoByIndex(this.selectedPetItemIndex)
    if _petInfo and #_petInfo.genes == PetManager.PetPart.Ornament then
        local _petModelInfo = {}
        _petModelInfo.parentGO = this.Model_Pet
        _petModelInfo.petId = _petInfo.petId
        if this.PetModelGO and this.goWrapper then
            _petModelInfo.modelGO = this.PetModelGO
            _petModelInfo.modelWrapper = this.goWrapper
            PetManager.PetModelRefresh(_petModelInfo, _petInfo.genes, true)
        else
            this:removePetModel()
            resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
                this.PetModelGO = newObject(go)
                
                local com = this.PetModelGO:GetComponent("SkeletonGraphic")

                if this.PetModelGO and com then
                    com.material = resMgr.Pet_Copy_Mat
                    com.material:SetFloat("_StencilComp",8)
                    com.material:SetFloat("_Stencil",1);
                end

                local _modelWrapper = this.goWrapper
                this.goWrapper = GoWrapper.New()
                _petModelInfo.modelGO = this.PetModelGO
                _petModelInfo.modelWrapper = this.goWrapper
                PetManager.PetModelFirstSet(_petModelInfo, _petInfo.genes, true, Vector3.New(0, 0, 0), Vector3.New(1.2, 1.2, 1),3)
                if _modelWrapper then
                    _modelWrapper:Dispose()
                end
            end)
        end
    end
end

function this.initModelTable()
    if this.petListModelTable == nil then
        this.petListModelTable = {}
    end
end
function this.removeModelTable()
    if this.petListModelTable then
        for index, _ in pairs(this.petListModelTable) do
            this.removePetListModelByIndex(index)
        end
    end
end
function this.removePetListModelByIndex(index)
    if this.petListModelTable[index].modelGO then
        destroy(this.petListModelTable[index].modelGO)    
    end
    this.petListModelTable[index].modelGO = nil
    this.petListModelTable[index].parentGO = nil
    this.petListModelTable[index].petId = nil

end
function this.showFamilyTreePetModel(petListIndex, parentGO, petId, genes)
    if this.petListModelTable[petListIndex] == nil then
        this.petListModelTable[petListIndex] = {}
    end
    --显示宠物模型
    if this.petListModelTable[petListIndex].modelGO and this.petListModelTable[petListIndex].petId == petId then
        PetManager.PetModelRefresh(this.petListModelTable[petListIndex], genes, false)
    else
        this.removePetListModelByIndex(petListIndex)
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
             local n_go = newObject(go)

            this.petListModelTable[petListIndex].modelGO = n_go
            this.petListModelTable[petListIndex].parentGO = parentGO
            local _modelWrapper = this.petListModelTable[petListIndex].modelWrapper
            this.petListModelTable[petListIndex].modelWrapper = GoWrapper.New()
            this.petListModelTable[petListIndex].petId = petId
            PetManager.PetModelFirstSet(this.petListModelTable[petListIndex], genes, false, Vector3.New(0, 0, 0), Vector3.New(0.55, 0.55, 1))
            if _modelWrapper then
                _modelWrapper:Dispose()
            end

            local com = n_go:GetComponent("SkeletonGraphic")
            if n_go and com then
                com.material:SetFloat("_StencilComp",3)
                com.material:SetFloat("_Stencil",1);
            end
            
        end)
    end
end
return PetSelect